//获取正在进行的拼团列表信息
function inProcessList(pageNo,pageSize, callback) {
  wx.request({
    url: getApp().globalData.host + 'pintuan/inProcessList',
    header: getApp().globalData.header,
    data: { pageNo: pageNo, pageIndex: pageSize },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data.records) != 'undefined' && res.data.records.length > 0) {
        for(let i = 0;i<res.data.records.length;i++){
          if(typeof(res.data.records[i].imgUrl)!='undefined'&&res.data.records[i].imgUrl.indexOf('img/')<2){ //处理图片兼容性
            res.data.records[i].imgUrl = getApp().globalData.imgHost + res.data.records[i].imgUrl;
          }
        }
        callback(res.data.records);
      }
    }
  })
}

//获取即将开始的拼团列表信息
function newList(pageNo, pageSize, callback) {
  wx.request({
    url: getApp().globalData.host + 'pintuan/newList',
    header: getApp().globalData.header,
    data: { pageNo: pageNo, pageIndex: pageSize },
    method:'POST',
    success: function (res) {
      if (typeof (res.data.records) != 'undefined' && res.data.records.length > 0) {
        for(let i = 0;i<res.data.records.length;i++){
          if(typeof(res.data.records[i].imgUrl)!='undefined'&&res.data.records[i].imgUrl.indexOf('img/')<2){ //处理图片兼容性
            res.data.records[i].imgUrl = getApp().globalData.imgHost + res.data.records[i].imgUrl;
          }
        }
        callback(res.data.records);
      }
    }
  })
}

/**
 * [查询拼团详情]
 * @param  {[type]}   itemId   [拼团ID]
 * @param  {Function} callback [callback]
 * @return {[type]}            [拼团详情]
 */
function item(itemId, callback) {
  if (typeof (itemId) == 'undefined') return null;
  wx.request({
    url: getApp().globalData.host + 'pintuan/statusForDetail',
    header: getApp().globalData.header,
    data: {itemId: itemId },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data != '') {
        if(typeof(res.data.imgUrl)!='undefined'&&res.data.imgUrl.indexOf('img/')<2){ //处理图片兼容性
            res.data.imgUrl = getApp().globalData.imgHost + res.data.imgUrl;
          }
        callback(res.data);
      }
    }
  })
}

/**
 * [立即购买]
 * @param  {[type]}   itemId   [拼团ID]
 * @param  {[type]}   quantity [拼团数量]
 * @param  {Function} callback [description]
 * @return {[type]}            [orderId]
 */
function buyNow(itemId,quantity,callback){
  if (typeof (itemId) == 'undefined'||typeof (quantity) == 'undefined'||Number(quantity)<1) return null;
  //添加动画
  wx.showLoading({
    title: '处理中...',
  })
  wx.request({
    url: getApp().globalData.host + 'order/confirmPintuan.html',
    header: getApp().globalData.header,
    data: { itemId: itemId,quantity:quantity},
    method: 'POST',
    success: function (res) {
      if (typeof (res.data.orderId) != 'undefined' && res.data.orderId != '') {
        callback(res.data.orderId);
      }
      else{
        wx.showModal({
          title: '拼团失败',
          showCancel:false
        })
      }
    },
    fail: function (res) { 
      wx.showModal({
        title:  '拼团失败',
        content:res.data.errorMsg,
        showCancel:false
      })
    },complete:function(res){
      wx.hideLoading();
    }
  })
}

/**
 * [SKU是否参与拼团]
 * @param  {[type]}   skuId    [skuID]
 * @param  {Function} callback [description]
 * @return {[type]}            [拼团信息，若无拼团，则不返回]
 */
function skuAndPintuan(skuId,callback){
  if (typeof (skuId) == 'undefined') return null;
  wx.request({
    url: getApp().globalData.host + 'pintuan/hasPintuan',
    header: getApp().globalData.header,
    data: {skuId: skuId },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && typeof (res.data.itemId) !='undefined') {
        callback(res.data);
      }
    }
  })
}

module.exports = {
  inProcessList: inProcessList,
  newList: newList,
  item: item,
  buyNow:buyNow,
  skuAndPintuan:skuAndPintuan
}